package com.whlilong.jxc.app;

import java.util.List;

import org.eclipse.jface.action.IContributionItem;
import org.eclipse.jface.action.ICoolBarManager;
import org.eclipse.jface.action.IStatusLineManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;
import org.eclipse.ui.internal.WorkbenchWindow;

import com.whlilong.jxc.constants.IAppConstants;
import com.whlilong.jxc.model.Regist;
import com.whlilong.jxc.sys.CacheImage;
import com.whlilong.jxc.sys.ExtentionInfo;
import com.whlilong.jxc.sys.HookSysTray;
import com.whlilong.jxc.sys.IImageKey;

@SuppressWarnings("restriction")
public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {

	private HookSysTray hookSysTray;

	public ApplicationWorkbenchWindowAdvisor(
			IWorkbenchWindowConfigurer configurer) {
		super(configurer);
	}

	public ActionBarAdvisor createActionBarAdvisor(
			IActionBarConfigurer configurer) {
		return new ApplicationActionBarAdvisor(configurer);
	}

	public void preWindowOpen() {
		IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
//		configurer.setInitialSize(new Point(1024, 768));
		configurer.setShowCoolBar(true);
		configurer.setShowStatusLine(true);
		configurer.setShowProgressIndicator(true);
		List<Regist> list = ExtentionInfo.getInstance().getRegist();
		if (list.get(0).getStatus().equals("0"))
			configurer.setTitle("立隆商务信息管理平台(试用版)");
		else
			configurer.setTitle("立隆商务信息管理平台");

	}

	public void postWindowCreate() {
		super.postWindowCreate();
		WorkbenchWindow activeWorkbenchWindow = (WorkbenchWindow) PlatformUI
				.getWorkbench().getActiveWorkbenchWindow();
		ICoolBarManager coolManager = activeWorkbenchWindow.getActionBars()
				.getCoolBarManager();
		IContributionItem items[] = coolManager.getItems();
		for (IContributionItem item : items) {
			if ("org.eclipse.ui.edit.text.actionSet.annotationNavigation"
					.equals(item.getId())
					|| "org.eclipse.ui.edit.text.actionSet.navigation"
							.equals(item.getId())) {
				coolManager.remove(item);
			}
		}
		coolManager.update(true);

		// 设置打开时最大化窗口
		getWindowConfigurer().getWindow().getShell().setMaximized(true);
	}

	public void postWindowOpen() {
//		 Shell shell = getWindowConfigurer().getWindow().getShell();
//		 Rectangle screenSize = Display.getDefault().getClientArea();
//		 Rectangle frameSize = shell.getBounds();
//		 shell.setLocation((screenSize.width -frameSize.width)>>1,
//		 (screenSize.height-frameSize.height)>>1);
//
//		 System.out.println(screenSize.width);
//		 System.out.println(screenSize.height);
//		 System.out.println(frameSize.width);
//		 System.out.println(frameSize.height);
//		 System.out.println((screenSize.width -frameSize.width)>>1);
//		 System.out.println((screenSize.height-frameSize.height)>>1);
		createSystemTray();

		// 设置状态栏信息
		IStatusLineManager statusline = getWindowConfigurer()
				.getActionBarConfigurer().getStatusLineManager();
		statusline.setMessage(CacheImage.getINSTANCE().getImage(
				IAppConstants.APPLICATION_ID, IImageKey.user16),
				"Powered by AuZn@武汉立隆商务信息管理系统");
	}

	public boolean preWindowShellClose() {
		hookSysTray.windowMinimized(getWindowConfigurer().getWindow()
				.getShell());
		return false;
	}

	public void dispose() {
		hookSysTray.dispose();
		// CacheImage.getINSTANCE().dispose();
	}

	private void createSystemTray() {
		hookSysTray = new HookSysTray();
		hookSysTray.createSysTray(getWindowConfigurer().getWindow());
	}
}
